package com.flymobi.fw.spring;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.flymobi.fw.util.Config;

/**
 * 
 * 
 * Title:
 * 
 * Description:
 * 
 * Copyright: Copyright (c) 2008
 *  
 * @author haoxz11
 * @created Mar 7, 2008 11:21:21 AM
 * @version $Id: ServiceLocator.java,v 1.1 2008/03/26 06:24:11 airsou Exp $
 */
public class ServiceLocator {

	private static ApplicationContext context;

	private static Logger log = Logger.getLogger(ServiceLocator.class);

	static {
		long startTime = System.currentTimeMillis();
		String[] configs;
		if (System.getProperty("spring.configfile") != null) {
			configs = StringUtils.split(
					System.getProperty("spring.configfile"), ",");
		} else {
			configs = Config.getStringArray("sys.spring.configfile");
		}

		context = new ClassPathXmlApplicationContext(configs);
		long elapsedTime = System.currentTimeMillis() - startTime;
		log.info("Spring initialization completed in " + elapsedTime + " ms");
		System.err.println("Spring initialization completed in " + elapsedTime
				+ " ms");
	}

	public static void init() {
		// �Ѿ���static�г�ʼ����
	} 

	public static ApplicationContext getContext() {
		return context;
	}

	public static void setContext(ApplicationContext context) {
		ServiceLocator.context = context;
	}

	public static void destroy() {
		context = null;
	}

	public static Object getService(String beanName) {
		return context.getBean(beanName);
	}
}